//选择收货地址页
const app = getApp()
var server = require('../../utils/server');
var common = require('../../utils/common');
var amapFile = require('../../libs/amap-wx.js');
var hideTimer = null;
Page({
  data: {
    nearbyBuildings: [],
    showNearBy: true,
    addressList: [],
    selectedAddr: "",
    city: "",
    isDeliverable: true,
    curla: "",
    curlon: "",
    searchValue: "",
    showCity: false,
    cityList: [],
    curCity: "定位中",
    isLogin: true,
    showLoginPop: false,
    locatecity: "",
    isDisableLog: false
  },
  onUnload: function() {
    app.globalData.searchAddrCity = "";
    wx.removeStorageSync('updateStoreArry');
  },
  onShow: function() {
    wx.getStorage({
      key: 'isDeliverable',
      success: res => {
        if (res.data == "0") {
          this.setData({
            isDeliverable: true
          })
        } else {
          this.setData({
            isDeliverable: false
          })
        }
      },
    })
    this.locateCurAddress();
    common.checkLogin().then(res => {
      if (res.errno == "0") {
        wx.setStorage({
          key: "sso",
          data: res.data.sso,
          success: res => {
            this.setData({
              isLogin: true
            })
            app.globalData.isGetWxUserinfo = true;
            server.postJSON(common.storePath + 'address/listforcity/', {
              "city": app.globalData.locateCity,
              "type": 1
            }, res => {
              if (res.data.errno == '0') {
                this.setData({
                  addressList: res.data.data
                })
              } else {
                common.showErrorToast(res.errmsg)
              }
            })
          },
          fail: res => {
            this.setData({
              isLogin: false
            })
          }
        })
      } else if ((res.errMsg).indexOf('getUserInfo:fail') > -1) {
        this.setData({
          isLogin: false
        })
      } else {
        common.showErrorToast(res.errmsg)
      }
    })
  },
  onHide() {
    app.globalData.searchAddrCity = "";
    clearTimeout(hideTimer);
  },
  hideToast() {
    hideTimer = setTimeout(function() {
      wx.hideToast()
    }, 3500)
  },
  showLoginPop() {
    this.setData({
      showLoginPop: true
    })
  },
  login(e) {
    this.setData({
      isDisableLog: true
    })
    if (e.detail.errMsg == "getUserInfo:ok") {
      common.checkLogin().then(res => {
        this.setData({
          isDisableLog: false
        })
        if (res.errno == "0") {
          this.setData({
            showLoginPop: false,
            isLogin: true
          })
          wx.setStorage({
            key: "sso",
            data: res.data.sso,
            success: res => {
              server.postJSON(common.storePath + 'address/listforcity/', {
                "city": app.globalData.locateCity,
                "type": 1
              }, res => {

                if (res.data.errno == '0') {
                  this.setData({
                    addressList: res.data.data
                  })
                } else {
                  common.showErrorToast(res.errmsg)
                }
              })
            }
          })
          if (JSON.stringify(res.data.member) != "{}") {
            this.storageUserinfo(res.data);
          }
        } else if ((res.errMsg).indexOf('getUserInfo:fail') > -1) {
          this.setData({
            showLoginPop: true
          })
        } else {
          common.showErrorToast(res.errmsg)
        }
      })

    } else {
      this.setData({
        isDisableLog: false
      })
      wx.showToast({
        icon: 'none',
        title: '需要授权用户信息使用元初到家！'
      })
    }
  },
  storageUserinfo(data) {
    wx.setStorage({
      key: "userInfo",
      data: data
    })
  },
  locateCurAddress() {
    var gdMap = new amapFile.AMapWX({
      key: common.gdKey
    });
    gdMap.getRegeo({
      success: res => {
        this.data.curla = res[0].latitude;
        this.data.curlon = res[0].longitude;
        this.setData({
          location: res[0].regeocodeData.pois[0].name,
          locatecity: res[0].regeocodeData.addressComponent.city,
          curCity: app.globalData.searchAddrCity == "" ? res[0].regeocodeData.addressComponent.city : app.globalData.searchAddrCity
        })

        common.getPlaceAround(res[0].longitude, res[0].latitude).then(res => {
          this.setData({
            nearbyBuildings: res.data
          })
        })
      },
      fail: info => {
        common.showErrorToast('定位中...')
      }
    })
  },
  searchAddress(e) {
    this.setData({
      searchValue: e.detail.value
    })
    if (e.detail.value != " " && e.detail.value.length != 0) {
      common.searchPlace(e.detail.value, this.data.curCity).then(res => {
        this.setData({
          resultBuildings: res.data,
          showNearBy: false
        })
      })
    }
  },
  selectAddr(e) {
    if (e.currentTarget.dataset.status != "1") {
      common.showToast("该地址无法配送!");
      this.hideToast();
    } else {
      app.globalData.refreshTag = true; //返回是否刷新商品列表页

      app.globalData.skipType = 2;
      app.globalData.location = e.currentTarget.dataset.name;
      app.globalData.latitude = e.currentTarget.dataset.la;
      app.globalData.longitude = e.currentTarget.dataset.lon;
      app.globalData.city = e.currentTarget.dataset.city;
      app.globalData.addrId = null;
      app.globalData.companyid = "";
      app.globalData.storeType = "";
      wx.setStorage({
        key: 'companyid',
        data: app.globalData.companyid,
      })
      wx.setStorage({
        key: "storeId",
        data: e.currentTarget.dataset.storeid
      })
      wx.navigateBack();
    }
  },
  chooseNewAddr(e) {
    app.globalData.refreshTag = true; //返回是否刷新商品列表页
    app.globalData.skipType = 1;
    app.globalData.latitude = e.currentTarget.dataset.la;
    app.globalData.longitude = e.currentTarget.dataset.lon;
    app.globalData.location = e.currentTarget.dataset.name;
    app.globalData.addrId = e.currentTarget.dataset.id;
    app.globalData.city = e.currentTarget.dataset.city;
    app.globalData.companyid = "";
    app.globalData.storeType = "";
    wx.setStorage({
      key: 'companyid',
      data: app.globalData.companyid,
    })
    wx.navigateBack();
  },
  chooseCur(e) {
    app.globalData.refreshTag = true; //返回是否刷新商品列表页
    app.globalData.skipType = 1;
    app.globalData.latitude = this.data.curla;
    app.globalData.longitude = this.data.curlon;
    app.globalData.location = e.currentTarget.dataset.cur;
    app.globalData.city = e.currentTarget.dataset.city;
    app.globalData.addrId = null;
    app.globalData.companyid = "";
    app.globalData.storeType = "";
    wx.setStorage({
      key: 'companyid',
      data: app.globalData.companyid,
    })
    wx.navigateBack();
  },
  clearInput() {
    this.setData({
      searchValue: "",
      showNearBy: true
    })
  },
  showCityList() {
    wx.navigateTo({
      url: '../../pages/common/cityList?fromtype=searchaddr',
    })
  },
  hideLoginPop() {
    this.setData({
      showLoginPop: false
    })
  },
  showAgreement() {
    wx.navigateTo({
      url: '../../pages/common/agreement',
    })
  }
})